/**
 !! 随便弄了一下，只是为了 目前项目的使用.过几天会 完善
 !! 加入单例等
 
 有问题可以联系 邮箱 906037367@qq.com
 QQ               906037367
 

 */

#import "PellTableViewSelect.h"
#import "Skin.h"
#import <UIKit_Category.h>

#define  LeftView 10.0f
#define  TopToView 10.0f
@interface  PellTableViewSelect()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,copy) NSArray *selectData;
@property (nonatomic,copy) void(^action)(NSInteger index);
@property (nonatomic,copy) NSArray * imagesData;

@property (nonatomic, assign) BOOL isShowTriangle;
@property (nonatomic, assign) BOOL isAnimate;

@end



PellTableViewSelect * backgroundView;
UITableView * tableView;
UIImageView * backImageView;

@implementation PellTableViewSelect


- (instancetype)init{
    if (self = [super init]) {
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}

+ (void)addPellTableViewSelectWithWindowFrame:(CGRect)frame
                                   selectData:(NSArray *)selectData
                                       images:(NSArray *)images
                                       action:(void(^)(NSInteger index))action animated:(BOOL)animate
{
    if (backgroundView != nil) {
        [PellTableViewSelect hiden];
    }
    UIWindow *win = [[[UIApplication sharedApplication] windows] firstObject];
    
    backgroundView = [[PellTableViewSelect alloc] initWithFrame:win.bounds];
    backgroundView.action = action;
    backgroundView.imagesData = images;
    backgroundView.selectData = selectData;
    backgroundView.backgroundColor = [UIColor colorWithHue:0
                                                saturation:0
                                                brightness:0 alpha:0.1];
    [win addSubview:backgroundView];
    backgroundView.isShowTriangle = YES;
    [backgroundView setNeedsDisplay];
    // TAB
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(  [UIScreen mainScreen].bounds.size.width - 80 , 70.0 -  20.0 * selectData.count + (27 * 0.5), frame.size.width, 40 * selectData.count) style:0];
    tableView.dataSource = backgroundView;
    tableView.delegate = backgroundView;
    tableView.backgroundColor = [UIColor clearColor];
//    tableView.layer.cornerRadius = 3.0f;
//    tableView.backgroundColor = [UIColor colorWithHexString:@"#31CC9A"];
//    tableView.backgroundView = [UIImage skin:@"img_deviceCtrl_navRightBtn_back"];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    tableView.rowHeight = 40;
    [win addSubview:tableView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackgroundClick)];
    [backgroundView addGestureRecognizer:tap];
    backgroundView.action = action;
    backgroundView.selectData = selectData;
    backgroundView.isAnimate = animate;
    
    backImageView = [[UIImageView alloc]initWithImage:[UIImage skin:@"img_deviceCtrl_navRightBtn_back.png"]];
    [backgroundView insertSubview:backImageView belowSubview:tableView];
    backImageView.frame = tableView.frame;
    backImageView.height += 27;
    backImageView.y -= 27 * 1.25;
    backImageView.transform =CGAffineTransformMakeScale(0.0001, 0.0001);
    backImageView.layer.anchorPoint = CGPointMake(1.0, 0);
    // 定点
    tableView.layer.anchorPoint = CGPointMake(1.0, 0);
    tableView.transform =CGAffineTransformMakeScale(0.0001, 0.0001);
    
    if (animate == YES) {
        backgroundView.alpha = 0;
        [UIView animateWithDuration:0.3 animations:^{
            backgroundView.alpha = 1;
            tableView.transform = CGAffineTransformMakeScale(1.0, 1.0);
            backImageView.transform = CGAffineTransformMakeScale(1.0, 1.0);
        }];
    }else{
        backgroundView.alpha = 1;
        tableView.transform = CGAffineTransformMakeScale(1.0, 1.0);
        backImageView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    }
   
}
+ (void)tapBackgroundClick
{
    [PellTableViewSelect hiden];
}
+ (void)hiden
{
    if (backgroundView != nil) {
        backgroundView.isShowTriangle = false;
        [backgroundView setNeedsDisplay];
        if (backgroundView.isAnimate) {
            
            [UIView animateWithDuration:0.3 animations:^{
                //            UIWindow * win = [[[UIApplication sharedApplication] windows] firstObject];
                //            tableView.frame = CGRectMake(win.bounds.size.width - 35 , 64, 0, 0);
                backgroundView.alpha = 0;
                tableView.transform = CGAffineTransformMakeScale(0.000001, 0.0001);
                backImageView.transform = CGAffineTransformMakeScale(0.000001, 0.0001);
            } completion:^(BOOL finished) {
                [backgroundView removeFromSuperview];
                [tableView removeFromSuperview];
                [backImageView removeFromSuperview];
                tableView = nil;
                backgroundView = nil;
                backImageView = nil;
            }];
        }else{
//            [UIView animateWithDuration:0.3 animations:^{
                //            UIWindow * win = [[[UIApplication sharedApplication] windows] firstObject];
                //            tableView.frame = CGRectMake(win.bounds.size.width - 35 , 64, 0, 0);
                backgroundView.alpha = 0;
                tableView.transform = CGAffineTransformMakeScale(0.000001, 0.0001);
            backImageView.transform = CGAffineTransformMakeScale(0.000001, 0.0001);
//            } completion:^(BOOL finished) {
                [backgroundView removeFromSuperview];
                [tableView removeFromSuperview];
                tableView = nil;
                backgroundView = nil;
                            backImageView = nil;
//            }];
        }
    }
   
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _selectData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier = @"PellTableViewSelectIdentifier";
    UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:Identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:Identifier];
    }
    cell.imageView.image = self.imagesData[indexPath.row];//[UIImage imageNamed:];
    cell.textLabel.text = _selectData[indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor clearColor];
    //        cell.backgroundColor = [UIColor colorWithHexString:@"#31CC9A"];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.action) {
        self.action(indexPath.row);
    }
    [PellTableViewSelect hiden];
}

#pragma mark 绘制三角形
//- (void)drawRect:(CGRect)rect
//{
//    if (backgroundView.isShowTriangle == false) {
//        return;
//    }
//    //    [colors[serie] setFill];
//    // 设置背景色
//    [[UIColor colorWithHexString:@"#31CC9A"] set];
//    //拿到当前视图准备好的画板
//    
//    CGContextRef  context = UIGraphicsGetCurrentContext();
//    
//    //利用path进行绘制三角形
//    
//    CGContextBeginPath(context);//标记
//    CGFloat location = [UIScreen mainScreen].bounds.size.width;
//    CGContextMoveToPoint(context,
//                         location -  LeftView - 10, 70);//设置起点
//    
//    CGContextAddLineToPoint(context,
//                             location - 2*LeftView - 10 ,  60);
//    
//    CGContextAddLineToPoint(context,
//                            location - TopToView * 3 - 10, 70);
//    
//    CGContextClosePath(context);//路径结束标志，不写默认封闭
//    
//    [[UIColor colorWithHexString:@"#31CC9A"] setFill];  //设置填充色
//    
//    [[UIColor colorWithHexString:@"#31CC9A"] setStroke]; //设置边框颜色
//    
//    CGContextDrawPath(context,
//                      kCGPathFillStroke);//绘制路径path
//    
////    [self setNeedsDisplay];
//}

@end
